﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using WISD.DBUtility.DBOperate;

namespace WISD.CarFriend.BLL.OA
{
    public class CardConsumeItemModule
    {

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_CF_OA_CardConsumeItem");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(WISD.CarFriend.Model.CFModel.T_CF_OA_CardConsumeItem model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_CF_OA_CardConsumeItem(");
            strSql.Append("MemberCardTypeId,ConsumeItem,ConsumeCount,Creator,CreateDate,Reviser,ReviseDate,Memo,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8)");
            strSql.Append(" values (");
            strSql.Append("@MemberCardTypeId,@ConsumeItem,@ConsumeCount,@Creator,@CreateDate,@Reviser,@ReviseDate,@Memo,@Memo1,@Memo2,@Memo3,@Memo4,@Memo5,@Memo6,@Memo7,@Memo8)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@MemberCardTypeId", SqlDbType.Int,4),
					new SqlParameter("@ConsumeItem", SqlDbType.NVarChar,100),
					new SqlParameter("@ConsumeCount", SqlDbType.Int,4),
					new SqlParameter("@Creator", SqlDbType.NVarChar,50),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@Reviser", SqlDbType.NVarChar,50),
					new SqlParameter("@ReviseDate", SqlDbType.DateTime),
					new SqlParameter("@Memo", SqlDbType.NVarChar,500),
					new SqlParameter("@Memo1", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo2", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo3", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo4", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo5", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo6", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo7", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo8", SqlDbType.NVarChar,100)};
            parameters[0].Value = model.MemberCardTypeId;
            parameters[1].Value = model.ConsumeItem;
            parameters[2].Value = model.ConsumeCount;
            parameters[3].Value = model.Creator;
            parameters[4].Value = model.CreateDate;
            parameters[5].Value = model.Reviser;
            parameters[6].Value = model.ReviseDate;
            parameters[7].Value = model.Memo;
            parameters[8].Value = model.Memo1;
            parameters[9].Value = model.Memo2;
            parameters[10].Value = model.Memo3;
            parameters[11].Value = model.Memo4;
            parameters[12].Value = model.Memo5;
            parameters[13].Value = model.Memo6;
            parameters[14].Value = model.Memo7;
            parameters[15].Value = model.Memo8;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(WISD.CarFriend.Model.CFModel.T_CF_OA_CardConsumeItem model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_CF_OA_CardConsumeItem set ");
            strSql.Append("MemberCardTypeId=@MemberCardTypeId,");
            strSql.Append("ConsumeItem=@ConsumeItem,");
            strSql.Append("ConsumeCount=@ConsumeCount,");
            strSql.Append("Creator=@Creator,");
            strSql.Append("CreateDate=@CreateDate,");
            strSql.Append("Reviser=@Reviser,");
            strSql.Append("ReviseDate=@ReviseDate,");
            strSql.Append("Memo=@Memo,");
            strSql.Append("Memo1=@Memo1,");
            strSql.Append("Memo2=@Memo2,");
            strSql.Append("Memo3=@Memo3,");
            strSql.Append("Memo4=@Memo4,");
            strSql.Append("Memo5=@Memo5,");
            strSql.Append("Memo6=@Memo6,");
            strSql.Append("Memo7=@Memo7,");
            strSql.Append("Memo8=@Memo8");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@MemberCardTypeId", SqlDbType.Int,4),
					new SqlParameter("@ConsumeItem", SqlDbType.NVarChar,100),
					new SqlParameter("@ConsumeCount", SqlDbType.Int,4),
					new SqlParameter("@Creator", SqlDbType.NVarChar,50),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@Reviser", SqlDbType.NVarChar,50),
					new SqlParameter("@ReviseDate", SqlDbType.DateTime),
					new SqlParameter("@Memo", SqlDbType.NVarChar,500),
					new SqlParameter("@Memo1", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo2", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo3", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo4", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo5", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo6", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo7", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo8", SqlDbType.NVarChar,100),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.MemberCardTypeId;
            parameters[1].Value = model.ConsumeItem;
            parameters[2].Value = model.ConsumeCount;
            parameters[3].Value = model.Creator;
            parameters[4].Value = model.CreateDate;
            parameters[5].Value = model.Reviser;
            parameters[6].Value = model.ReviseDate;
            parameters[7].Value = model.Memo;
            parameters[8].Value = model.Memo1;
            parameters[9].Value = model.Memo2;
            parameters[10].Value = model.Memo3;
            parameters[11].Value = model.Memo4;
            parameters[12].Value = model.Memo5;
            parameters[13].Value = model.Memo6;
            parameters[14].Value = model.Memo7;
            parameters[15].Value = model.Memo8;
            parameters[16].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_CF_OA_CardConsumeItem ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_CF_OA_CardConsumeItem ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public WISD.CarFriend.Model.CFModel.T_CF_OA_CardConsumeItem GetModel(int Id)
        {
           
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,MemberCardTypeId,ConsumeItem,ConsumeCount,Creator,CreateDate,Reviser,ReviseDate,Memo,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8 from T_CF_OA_CardConsumeItem ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            WISD.CarFriend.Model.CFModel.T_CF_OA_CardConsumeItem model = new WISD.CarFriend.Model.CFModel.T_CF_OA_CardConsumeItem();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MemberCardTypeId"] != null && ds.Tables[0].Rows[0]["MemberCardTypeId"].ToString() != "")
                {
                    model.MemberCardTypeId = int.Parse(ds.Tables[0].Rows[0]["MemberCardTypeId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ConsumeItem"] != null && ds.Tables[0].Rows[0]["ConsumeItem"].ToString() != "")
                {
                    model.ConsumeItem = ds.Tables[0].Rows[0]["ConsumeItem"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ConsumeCount"] != null && ds.Tables[0].Rows[0]["ConsumeCount"].ToString() != "")
                {
                    model.ConsumeCount = int.Parse(ds.Tables[0].Rows[0]["ConsumeCount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Creator"] != null && ds.Tables[0].Rows[0]["Creator"].ToString() != "")
                {
                    model.Creator = ds.Tables[0].Rows[0]["Creator"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CreateDate"] != null && ds.Tables[0].Rows[0]["CreateDate"].ToString() != "")
                {
                    model.CreateDate = DateTime.Parse(ds.Tables[0].Rows[0]["CreateDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Reviser"] != null && ds.Tables[0].Rows[0]["Reviser"].ToString() != "")
                {
                    model.Reviser = ds.Tables[0].Rows[0]["Reviser"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ReviseDate"] != null && ds.Tables[0].Rows[0]["ReviseDate"].ToString() != "")
                {
                    model.ReviseDate = DateTime.Parse(ds.Tables[0].Rows[0]["ReviseDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Memo"] != null && ds.Tables[0].Rows[0]["Memo"].ToString() != "")
                {
                    model.Memo = ds.Tables[0].Rows[0]["Memo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo1"] != null && ds.Tables[0].Rows[0]["Memo1"].ToString() != "")
                {
                    model.Memo1 = ds.Tables[0].Rows[0]["Memo1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo2"] != null && ds.Tables[0].Rows[0]["Memo2"].ToString() != "")
                {
                    model.Memo2 = ds.Tables[0].Rows[0]["Memo2"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo3"] != null && ds.Tables[0].Rows[0]["Memo3"].ToString() != "")
                {
                    model.Memo3 = ds.Tables[0].Rows[0]["Memo3"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo4"] != null && ds.Tables[0].Rows[0]["Memo4"].ToString() != "")
                {
                    model.Memo4 = ds.Tables[0].Rows[0]["Memo4"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo5"] != null && ds.Tables[0].Rows[0]["Memo5"].ToString() != "")
                {
                    model.Memo5 = ds.Tables[0].Rows[0]["Memo5"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo6"] != null && ds.Tables[0].Rows[0]["Memo6"].ToString() != "")
                {
                    model.Memo6 = ds.Tables[0].Rows[0]["Memo6"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo7"] != null && ds.Tables[0].Rows[0]["Memo7"].ToString() != "")
                {
                    model.Memo7 = ds.Tables[0].Rows[0]["Memo7"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo8"] != null && ds.Tables[0].Rows[0]["Memo8"].ToString() != "")
                {
                    model.Memo8 = ds.Tables[0].Rows[0]["Memo8"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,MemberCardTypeId,ConsumeItem,ConsumeCount,Creator,CreateDate,Reviser,ReviseDate,Memo,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8 ");
            strSql.Append(" FROM T_CF_OA_CardConsumeItem ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,MemberCardTypeId,ConsumeItem,ConsumeCount,Creator,CreateDate,Reviser,ReviseDate,Memo,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8 ");
            strSql.Append(" FROM T_CF_OA_CardConsumeItem ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_CF_OA_CardConsumeItem ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Id desc");
            }
            strSql.Append(")AS Row, T.*  from T_CF_OA_CardConsumeItem T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }


    }
}
